package com.lkd.controller;


import com.lkd.dto.LoginDto;
import com.lkd.entity.User;
import com.lkd.result.Result;
import com.lkd.service.LoginService;
import com.lkd.utils.JwtUtil;
import com.lkd.vo.LoginUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     * @param dto
     * @return
     */
    @PostMapping("/api/user-service/user/login")
    public Result login(@RequestBody LoginDto dto){
        //调用service登录
        User user = loginService.login(dto);
        //制作token
        Map<String,Object> map = new HashMap<>();
        map.put("empId", user.getId());
        String token = JwtUtil.createJWT("lkd", 1000000L, map);

        log.info("token:{}",map.get("token"));
        log.info("登录成功");
        //返回vo
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setRegionId(user.getRegionId().toString());
        loginUserVo.setToken(token);
        return Result.success(loginUserVo);
    }
}
